# -*- coding:UTF-8 -*-
import pygame
import pytmx
from pytmx.util_pygame import load_pygame
import tools.collision as collision
class Scene:
    '''
    基本场景定义
    '''
    def __init__(self, hero):
        '''
        场景初始化
        :param hero: 游戏主角英雄
        '''
        self.player = hero
        self.blockers_group = [] # 边界组
        self.sprites_group = [] #角色组
        self.portals_group = [] #门组

        self.curr_portal_name = 'none'
        self.curr_portal = False

        self.next_scene_id = None #下一个场景
        self.next_scenes = None #下一个场景组

        self.curr_dialog = False #当前是否说话
        self.curr_dialog_sprite = None  #当前说话的精灵
        self.curr_dialog_id  = 0 #当前说话的序列id
        self.dialogs = None #所有对话

        self.my_font = pygame.font.Font("resource/fonts/mini.ttf", 10)

    def init_self(self):
        '''
        初始化场景
        :return: 无
        '''
        self.set_tmx_path()
        self.init_scene()
        self.init_object()
        self.init_dialog()
        self.draw_scene()
        # self.collision = collision.CollisionTools(
        #     self.player, self.blockers_group, self.sprites_group, self.portals_group, self)

    def update(self, keys, mouse):
        '''
        场景更新
        :return: 无
        '''
        pass

    def set_tmx_path(self):
        '''
        设置tmx路径,需在派生类定义
        :return:
        '''
        pass

    def init_object(self):
        '''
        初始化场景中的对象,需在派生类定义
        :return:
        '''
        pass

    def init_dialog(self):
        '''
        初始话对话,,需在派生类定义
        :return:
        '''
        pass


    def init_scene(self):
        '''
        初始化场景基本图像
        :return: 无
        '''
        self.map = pytmx.TiledMap(self.path)
        self.display_width = self.map.width * self.map.tilewidth
        self.display_height = self.map.height * self.map.tileheight
        self.game_screen = pygame.Surface((self.display_width, self.display_height))

        self.dialog_imp = [None, None, None, None] #定义数组
        self.dialog_imp[0] = pygame.image.load("resource/graphics/dialog/1.png").convert()
        self.dialog_imp[1] = pygame.image.load("resource/graphics/dialog/2.png").convert()
        self.dialog_imp[2] = pygame.image.load("resource/graphics/dialog/3.png").convert()
        self.dialog_imp[3] = pygame.image.load("resource/graphics/dialog/4.png").convert()
        self.game_map = load_pygame(self.path)

    def draw_scene(self):
        '''
        绘制场景
        :return: 无
        '''
        ti = self.game_map.get_tile_image_by_gid
        for layer in self.game_map.visible_layers:
            if isinstance(layer, pytmx.TiledTileLayer):
                for x, y, gid in layer:
                    tile = ti(gid)
                    if tile and isinstance(tile, pygame.Surface):
                        self.game_screen.blit(tile, (x * self.game_map.tilewidth, y * self.game_map.tileheight))

    def draw_all_object(self, surface):
        '''
        绘制所有内容
        :param surfacce: 绘制的surface
        :return: 无
        '''
        show_screen = pygame.Surface((self.display_width, self.display_height))
        show_screen.blit(self.game_screen,(0, 0))
        self.draw_self_object(show_screen)
        self.player.draw_self(show_screen)
        # self.show_dialog()
        surface.blit(show_screen, (0, 0))

    def draw_self_object(self,surface):
        '''
        绘制所有精灵对象
        :return: 无
        '''
        for sprite in self.sprites_group:
            sprite.draw_self(surface)
        # for portal in self.portals_group:
        #     portal.draw_self(self.game_screen)

    def show_dialog(self, sprite, id):
        '''
        绘制与精灵对象的对话
        :param sprite: 对话的精灵
        :param id: 对话的序号
        :return: 无
        '''
        if self.curr_dialog == True:
            self.game_screen.blit(dialog_imp[0], sprite.pos)
            dialog = self.dialogs[sprite.name](id);
            text_surface = font.render(dialog, True, (0, 0, 255))
            self.game_screen.blit(text_surface, sprite.pos)
